package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.DeptMapper;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.*;
import com.itheima.service.EmpExprService;
import com.itheima.service.EmpService;
import io.micrometer.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
public class EmpServiceImpl extends ServiceImpl<EmpMapper, Emp> implements EmpService {
    @Autowired
    private EmpExprService empExprService;

    @Autowired
    private DeptMapper deptMapper;

    @Transactional
    @Override
    public void saveEmpAndEmpExpr(Emp emp) {
        save(emp);
        List<EmpExpr> exprList = emp.getExprList();
        exprList.forEach(v -> v.setEmpId(emp.getId()));
        empExprService.saveBatch(exprList);
    }

    @Override
    public PageResult<Emp> getPageResult(EmpQueryParam param) {
        String name = param.getName();
        Integer gender = param.getGender();
        LocalDate begin = param.getBegin();
        LocalDate end = param.getEnd();
        Page<Emp> empPage = new Page<>(param.getPage(), param.getPageSize());
        LambdaQueryWrapper<Emp> lambdaQueryWrapper = new LambdaQueryWrapper<Emp>()
                .eq(StringUtils.isNotBlank(name), Emp::getName, name)
                .eq(!Objects.isNull(gender), Emp::getGender, gender)
                .between(!Objects.isNull(begin) && !Objects.isNull(end), Emp::getEntryDate, begin, end);
        page(empPage, lambdaQueryWrapper);
        // 通过部门id查询部门名称
        List<Emp> empList = empPage.getRecords();
        List<Integer> deptIdlist = empList.stream().map(Emp::getDeptId).toList();
        if (!deptIdlist.isEmpty()) {
            List<Dept> deptList = deptMapper.selectBatchIds(deptIdlist);
            Map<Integer, String> deptMap = deptList.stream().collect(Collectors.toMap(Dept::getId, Dept::getName));
            empList.forEach(v -> v.setDeptName(deptMap.get(v.getDeptId())));
        }
        return new PageResult<>(empPage.getTotal(), empList);
    }

    @Transactional
    @Override
    public void removeEmpByIds(List<Integer> ids) {
        removeBatchByIds(ids);
        empExprService.remove(new LambdaQueryWrapper<EmpExpr>().in(EmpExpr::getEmpId, ids));
    }

    @Override
    public Emp getEmpAndExprById(Integer id) {
        Emp emp = getById(id);
        emp.setExprList(empExprService.list(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId, id)));
        return emp;
    }

    @Override
    public void updateEmp(Emp emp) {
        emp.setUpdateTime(LocalDateTime.now());
        updateById(emp);
        empExprService.remove(new LambdaQueryWrapper<EmpExpr>().eq(EmpExpr::getEmpId, emp.getId()));
        List<EmpExpr> exprList = emp.getExprList();
        if (exprList != null && !exprList.isEmpty()) {
            exprList.forEach(v -> v.setEmpId(emp.getId()));
            empExprService.saveBatch(exprList);
        }
    }
}
